﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket_Desktop.BL;

namespace ElectronicsMarket_Desktop
{
    public partial class frmNhanVien : Form
    {
        StaffBL staffbl;
        DepartmentBL departmentbl;
        public frmNhanVien()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblUser_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtHoDem_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            staffbl.Staff.STAFF_ID = Convert.ToInt32(txtMaNhanVien.Text);
            staffbl.Staff.STAFF_SYS_NAME = txtTenDangNhap.Text;
            staffbl.Staff.STAFF_PHONE = txtSoDienThoai.Text;
            staffbl.Staff.STAFF_PASSWORD = txtMatKhau.Text;
            staffbl.Staff.STAFF_MIDDLE_NAME = txtHoDem.Text;
            staffbl.Staff.STAFF_LEVEL = Convert.ToSByte(cboQuyen.Text);
            staffbl.Staff.STAFF_LAST_NAME = txtho.Text;
            staffbl.Staff.STAFF_FISRT_NAME = txtTen.Text;
            staffbl.Staff.STAFF_DAY_TO_WORK = dtimeNgayVaoLam.Value;
            staffbl.Staff.STAFF_ADDRESS = txtDiaChi.Text;
            staffbl.Staff.STAFF_DEPARMENT_ID = Convert.ToString(cbophongban.SelectedValue);
            staffbl.Staff.STAFF_DAY_ON_LEAVE = dtimengayketthuc.Value;
            if (cbogioitinh.SelectedItem.ToString() == "Nam")
                staffbl.Staff.STAFF_SEX = true;
            else
                staffbl.Staff.STAFF_SEX = false;
          if(  rdiothem.Checked == true)
            {
                staffbl.Staff.STAFF_ID = Convert.ToInt32( staffbl.getLastStaffId() + 1);   
                staffbl.insertStaff();
            }

          if (rdioXoa.Checked == true)
          {
              staffbl.deleteStaff();
          }
          if (rdioCapNhat.Checked == true)
          {
              staffbl.updateStaff();
          }
          DataBind();
        }
        public void DataBind()
        {

            grdnhanvien.AutoGenerateColumns = false;
            staffbl = new StaffBL();
            grdnhanvien.DataSource = staffbl.getAllStaff();
            //
            departmentbl= new DepartmentBL();
            cbophongban.DataSource = departmentbl.getAllDepartment();
            cbophongban.DisplayMember = "DEPARTMENT_NAME";
            cbophongban.ValueMember = "DEPARTMENT_ID";
        }

        private void grdnhanvien_SelectionChanged(object sender, EventArgs e)
        {
            txtMaNhanVien.Text = grdnhanvien.CurrentRow.Cells[0].Value.ToString();
            txtTenDangNhap.Text = grdnhanvien.CurrentRow.Cells[1].Value.ToString();
            txtMatKhau.Text = grdnhanvien.CurrentRow.Cells["colmatkhau"].Value.ToString();
            txtho.Text = grdnhanvien.CurrentRow.Cells["colho"].Value.ToString();
            txtHoDem.Text = grdnhanvien.CurrentRow.Cells["colhodem"].Value.ToString();
            txtTen.Text = grdnhanvien.CurrentRow.Cells["colten"].Value.ToString();
            txtSoDienThoai.Text =  grdnhanvien.CurrentRow.Cells["coldienthoai"].Value.ToString();
            if (grdnhanvien.CurrentRow.Cells["colgioitinh"].Value.ToString() == "False")
                cbogioitinh.Text = "Nam";
            else
                cbogioitinh.Text = "Nữ";
            cbophongban.SelectedValue = grdnhanvien.CurrentRow.Cells["colphongban"].Value;
            if (grdnhanvien.CurrentRow.Cells["colngayvaolam"].Value != null)
            {
                dtimeNgayVaoLam.Text = Convert.ToDateTime(grdnhanvien.CurrentRow.Cells["colngayvaolam"].Value).ToShortDateString();
            }
            if (grdnhanvien.CurrentRow.Cells["colngayketthuc"].Value != null)
            {
                dtimengayketthuc.Text = Convert.ToDateTime(grdnhanvien.CurrentRow.Cells["colngayketthuc"].Value).ToShortDateString();
            }
            cboQuyen.Text = grdnhanvien.CurrentRow.Cells["colquyen"].Value.ToString();
            txtDiaChi.Text = grdnhanvien.CurrentRow.Cells["coldiachi"].Value.ToString();


        }
    }
}
